// 页面导航工具类
const tabBarPages = ['/pages/home/home', '/pages/add/add', '/pages/shop/shop']

// 判断是否是 tabBar 页面
const isTabBarPage = (path) => tabBarPages.includes(path)

// 通用导航方法
const navigate = (path, params = {}) => {
	if (!path) return
	
	// 构建带参数的 URL
	const query = Object.keys(params)
		.map(key => `${key}=${encodeURIComponent(params[key])}`)
		.join('&')
	const url = query ? `${path}?${query}` : path
	
	// 根据页面类型选择导航方式
	if (isTabBarPage(path)) {
		uni.switchTab({
			url,
			fail: handleNavigateError
		})
	} else {
		uni.navigateTo({
			url,
			fail: handleNavigateError
		})
	}
}

// 返回上一页
const navigateBack = (delta = 1) => {
	uni.navigateBack({
		delta,
		fail: handleNavigateError
	})
}

// 处理导航错误
const handleNavigateError = (err) => {
	console.error('页面跳转失败:', err)
	uni.showToast({
		title: '页面跳转失败',
		icon: 'none'
	})
}

export default {
	navigate,
	navigateBack,
	isTabBarPage
} 